package com.yf.controller.api.article;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yf.annotation.IgnoreAuth;
import com.yf.annotation.LoginUser;
import com.yf.consts.DaoConst;
import com.yf.entity.ArticleEntity;
import com.yf.entity.UserEntity;
import com.yf.response.R;
import com.yf.service.article.ArticleService;
import com.yf.service.article.ShareService;
import com.yf.service.UserService;
import com.yf.vo.ShareVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequestMapping("/api/share")
@RestController
public class ApiShareController {

    /**
     * 所有文章列表
     * 传递当前页 page
     * 限制数量 limit
     * @return
     */
    @GetMapping("/articles")
    @IgnoreAuth
    public R listArticle(@RequestParam Map<String, Object> params){
        PageHelper.startPage(DaoConst.getPage(params),DaoConst.getLIMIT(params));
        List<ArticleEntity> articleList = articleService.queryList(params);
        return R.ok(new PageInfo(articleList));
    }

    /**
     * 通过分享,渲染文章
     * 文章内容，当前分享者，点赞人头像
     * @param
     * @return
     */
    @GetMapping("/article/{shareId}")
    public R listArticle(@PathVariable Long shareId,@LoginUser UserEntity userEntity){
        ShareVo shareVo = shareService.showArticle(shareId,userEntity.getId());
        return R.ok(shareVo);
    }

    /**
     * 点击公司首页进来的
     * 文章内容，当前分享者，点赞人头像
     * @param
     * @return
     */
    @GetMapping("/companyArticle/{articleId}")
    public R showArticleByArticleId(@PathVariable Long articleId,@LoginUser UserEntity userEntity){
        ShareVo shareVo = shareService.showArticleByArticleId(articleId,userEntity.getId());
        return R.ok(shareVo);
    }

    /**
     * 修改用户信息和创建分享
     * @param articleId
     * @return
     */
    @PostMapping("/create")
    public R share(@LoginUser UserEntity userEntity,Long articleId,String name,String info,String phone,String qrcode){
        return R.ok(shareService.createShare(userEntity.getId(),articleId,phone,name,qrcode,info));
    }

    /**
     * 点赞
     * @param articleId
     * @return
     */
    @PostMapping("/thumbUp")
    public R thumbUp(Long articleId,@LoginUser UserEntity userEntity){
        return R.ok(shareService.thumbUp(articleId,userEntity.getId()));
    }


    /**
     * 查询用户信息
     * @param
     * @return
     */
    @GetMapping("/user")
    public R getUser(@LoginUser UserEntity userEntity){
        return R.ok(userService.get(userEntity.getId()));
    }

    /**
     *
     * @param
     * @return
     */

    @Autowired
    private ArticleService articleService;

    @Autowired
    private ShareService shareService;

    @Autowired
    private UserService userService;


}
